/**
 * 后台API请求
 * @param {*对象参数} params 
 * @param {*回调函数} callback 
 */
export class Request {
  static HOST = 'https://weizhi.darenjian.com/api/pinduo/chou_award/';
  static HOSTPDUO = 'https://weizhi.darenjian.com/api/pinduo/';
  constructor(url = null, data) {
    this.url = url;
  }
  request(params, callback) {
    wx.request({
      url: `${Request.HOST}${this.url}`,
      method: 'POST',
      data: params(),
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': 'PHPSESSID=' + getApp().globalData.userid
      },
      success: (setData) => {
        setData.data.code == -1 || setData.statusCode != 200 ? new error() : new success();

        function success() {
          callback(setData);
        }

        function error() {
          wx.showToast({
            title: '登陆超时',
            icon: 'none',
            duration: 2000,
            mask: true
          })
          setTimeout(function () {
            // wx.redirectTo({
            //   url: '/pages/zhao_home/zhao_home'
            // })
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      }
    })
  }
  /**
   * 
   * @param {*获取 拼多多数据接口} params 
   */
  GetDeatilInfo(params,callback){
    wx.request({
      url: `${Request.HOSTPDUO}${this.url}`,
      method: 'POST',
      data: params(),
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': 'PHPSESSID=' + getApp().globalData.userid
      },
      success: (setData) => {
        setData.data.code == -1 || setData.statusCode != 200 ? new error() : new success();
        function success() {
          callback(setData);
        }
        function error() {
          wx.showToast({
            title: '登陆超时',
            icon: 'none',
            duration: 2000,
            mask: true
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000);
        }
      }
    });
  }
  /**
   * 文本内容鉴黄识别
   * @param {*文本内容} params 
   * @param {*回调函数} callback 
   */
  msgSecCheck(params,callback){
    wx.request({
      url: 'https://api.weixin.qq.com/wxa/msg_sec_check',
      method: 'POST',
      data: params(),
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': 'PHPSESSID=' + getApp().globalData.phpsessId
      },
      success: (setData) => {
        setData.data.code == -1 || setData.statusCode != 200 || setData.data.errcode == 87014 ? new error() : new success();
        function success() {
          callback(setData);
        }
        function error() {
          wx.showToast({
            title: '内容含有违法违规内容',
            icon: 'none',
            duration: 2000,
            mask: true
          })
          setTimeout(function () {
            wx.navigateBack({
              delta:1
            })
          }, 2000)
        }
      }
    });
  }
}